﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lottery.MediaData.Core {
    /// <summary>
    /// 表一：媒体数据 助手类
    /// </summary>
    public class MDHelper {
        /// <summary>
        /// 生成过滤表
        /// </summary>
        /// <param name="input"></param>
        /// <param name="ds"></param>
        /// <param name="fd"></param>
        /// <param name="subIndex"></param>
        /// <returns></returns>
        public static FTCellData Gen(FTCellData input, List<DS> ds, List<FD> fd, string subIndex) {
            int index = input.RowCount;
            int counter = 1;
            int lastCounter = fd.Count;
            foreach (var item in fd) {
                var dataInput = new List<string>();

                int mediaCount = item.MediaCount;

                dataInput.Add((++index).ToString());

                foreach (var data in ds) {

                    StringBuilder sb = new StringBuilder();
                    if (data.Three == mediaCount)
                        sb.Append("3");

                    if (data.One == mediaCount)
                        sb.Append("1");

                    if (data.Zero == mediaCount)
                        sb.Append("0");

                    string cell = sb.ToString();

                    dataInput.Add(cell);
                }
                StringBuilder line = new StringBuilder();
                line.Append(item.Low);
                line.Append("-");
                line.Append(item.Up);

                dataInput.Add(line.ToString());

                // ‘正确’列的占位符
                dataInput.Add(string.Empty);

                // 限制名称。(仅仅在开始一行和最后一行加上限制名称)
                if (counter == 1 || counter == lastCounter)
                    dataInput.Add(string.Format("{1} 表1-{0}", subIndex, item.MediaCount));
                else
                    dataInput.Add(item.MediaCount.ToString());

                counter++;

                input.AddRow(dataInput);
            }

            return input;
        }

        /// <summary>
        /// 合并行
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static FTCellData CombineLine(FTCellData input, List<FD> fs, CL c) {
            var oldCount = fs.Count;

            #region 合并数＝1的行合并
            var ls = new List<string>();
            var index = fs.Count + 1;
            ls.Add(index.ToString()); // 序号
            for (int i = 0; i < 14; i++)
                ls.Add(string.Empty);

            for (int row = 0; row < fs.Count; row++) {
                var f = fs[row];
                if (!f.IsAttended) continue;

                if (f.Count != 1) continue;

                for (int col = 1; col < 15; col++)
                    ls[col] = Combine(ls[col], input[col, row]);
            }

            var up = 0;
            for (int i = 1; i < 15; i++) {
                if (!string.IsNullOrEmpty(ls[i])) up++;
            }
            // 限制范围
            ls.Add(string.Format("0-{0}", up));
            // "正确个数"占位符
            ls.Add(string.Empty);
            // 限制名称
            ls.Add("合并数=1");

            input.AddRow(ls);
            #endregion

            #region 合并数＝2的行合并
            ls = new List<string>();
            index++;

            ls.Add(index.ToString()); // 序号
            for (int i = 0; i < 14; i++) {
                ls.Add(string.Empty);
            }

            for (int row = 0; row < fs.Count; row++) {
                var f = fs[row];
                if (!f.IsAttended) continue;

                if (f.Count != 2) continue;

                for (int col = 1; col < 15; col++)
                    ls[col] = Combine(ls[col], input[col, row]);
            }

            up = 0;
            for (int i = 1; i < 15; i++) {
                if (!string.IsNullOrEmpty(ls[i])) up++;
            }
            // 限制范围
            ls.Add(string.Format("0-{0}", up));
            // "正确个数"占位符
            ls.Add(string.Empty);
            // 限制名称
            ls.Add("合并数=2");

            input.AddRow(ls);
            #endregion

            #region 合并数＝3的行合并
            ls = new List<string>();

            index++;
            ls.Add(index.ToString()); // 序号
            for (int i = 0; i < 14; i++) {
                ls.Add(string.Empty);
            }

            for (int row = 0; row < fs.Count; row++) {
                var f = fs[row];
                if (!f.IsAttended) continue;

                if (f.Count != 3) continue;

                for (int col = 1; col < 15; col++)
                    ls[col] = Combine(ls[col], input[col, row]);
            }

            up = 0;
            for (int i = 1; i < 15; i++) {
                if (!string.IsNullOrEmpty(ls[i])) up++;
            }
            // 限制范围
            ls.Add(string.Format("0-{0}", up));
            // "正确个数"占位符
            ls.Add(string.Empty);
            // 限制名称
            ls.Add("合并数=3");

            input.AddRow(ls);
            #endregion

            #region 合并数＝1+2
            var ls12 = new List<string>();
            index++;
            ls12.Add(index.ToString()); // 序号
            for (int col = 1; col < 15; col++) {
                ls12.Add(Combine(input[col, oldCount], input[col, oldCount + 1]));
            }
            up = 0;
            for (int i = 1; i < 15; i++) {
                if (!string.IsNullOrEmpty(ls12[i])) up++;
            }
            // 限制范围
            ls12.Add(string.Format("0-{0}", up));
            // "正确个数"占位符
            ls12.Add(string.Empty);
            // 限制名称
            ls12.Add("合并数=1+2");

            input.AddRow(ls12);
            #endregion

            #region 合并数＝1+2+3
            var ls123 = new List<string>();
            index++;
            ls123.Add(index.ToString()); // 序号
            for (int col = 1; col < 15; col++) {
                ls123.Add(Combine(input[col, oldCount + 2], ls12[col]));
            }
            up = 0;
            for (int i = 1; i < 15; i++) {
                if (!string.IsNullOrEmpty(ls123[i])) up++;
            }
            // 限制范围
            ls123.Add(string.Format("0-{0}", up));
            // "正确个数"占位符
            ls123.Add(string.Empty);
            // 限制名称
            ls123.Add("合并数=1+2+3");

            input.AddRow(ls123);
            #endregion

            #region 区域1 合并
            var from1 = c.From1;
            var to1 = c.To1;
            var beginCombine = false;
            var lsRegion1 = new List<string>();
            index++;
            lsRegion1.Add(index.ToString()); // 序号
            if (from1 != int.MinValue) {
                for (int i = 0; i < oldCount; i++) {
                    if (fs[i].MediaCount == to1)
                        beginCombine = true;

                    if (!beginCombine) continue;

                    if (lsRegion1.Count == 1) {
                        for (int col = 1; col < 15; col++) {
                            lsRegion1.Add(input[col, i]);
                        }
                    } else {
                        for (int col = 1; col < 15; col++) {
                            lsRegion1[col] = Combine(lsRegion1[col], input[col, i]);
                        }
                    }

                    if (fs[i].MediaCount == from1) break;
                }
            } else {
                for (int i = 0; i < 14; i++) {
                    lsRegion1.Add(string.Empty);
                }
            }

            up = 0;
            for (int i = 1; i < 15; i++) {
                if (!string.IsNullOrEmpty(lsRegion1[i])) up++;
            }
            // 限制范围
            lsRegion1.Add(string.Format("0-{0}", up));
            // "正确个数"占位符
            lsRegion1.Add(string.Empty);
            // 限制名称
            lsRegion1.Add(string.Format("{0}～{1} 合并", from1, to1));

            input.AddRow(lsRegion1);
            #endregion

            #region 区域2 合并
            var from2 = c.From2;
            var to2 = c.To2;
            beginCombine = false;
            var lsRegion2 = new List<string>();
            index++;
            lsRegion2.Add(index.ToString()); // 序号
            if (from2 != int.MinValue) {
                for (int i = 0; i < oldCount; i++) {
                    if (fs[i].MediaCount == to2)
                        beginCombine = true;

                    if (!beginCombine) continue;

                    if (lsRegion2.Count == 1) {
                        for (int col = 1; col < 15; col++) {
                            lsRegion2.Add(input[col, i]);
                        }
                    } else {
                        for (int col = 1; col < 15; col++) {
                            lsRegion2[col] = Combine(lsRegion2[col], input[col, i]);
                        }
                    }

                    if (fs[i].MediaCount == from2) break;
                }
            } else {
                for (int i = 0; i < 14; i++) {
                    lsRegion2.Add(string.Empty);
                }
            }

            up = 0;
            for (int i = 1; i < 15; i++) {
                if (!string.IsNullOrEmpty(lsRegion2[i])) up++;
            }
            // 限制范围
            lsRegion2.Add(string.Format("0-{0}", up));
            // "正确个数"占位符
            lsRegion2.Add(string.Empty);
            // 限制名称
            lsRegion2.Add(string.Format("{0}～{1} 合并", from2, to2));

            input.AddRow(lsRegion2);
            #endregion

            return input;
        }

        /// <summary>
        /// 组合310字符串，例如:Combine("10", "30") == "310"
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        private static string Combine(string a, string b) {
            if (a == null) {
                a = string.Empty;
            }

            if (b == null) {
                b = string.Empty;
            }
            var temp = a + b;
            var chs = (from item in temp
                       orderby item descending
                       select item).Distinct();
            var sb = new StringBuilder();
            foreach (var item in chs) {
                sb.Append(item);
            }
            return sb.ToString();
        }
    }
}
